package qiujing.online.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import qiujing.online.pojo.Comment;
import qiujing.online.pojo.Praise;
import qiujing.online.pojo.Reply;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 评论操作类
 * Created by catt on 2018/12/7.
 */
@Service
public class CommentTemplate {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 新增一条评论
     * @param userId 评论人唯一标识
     * @param target 评论对象唯一标识
     * @param content 评论内容
     * @return
     */
    public Boolean add(String userId,String target,String content){
        Date date = new Date();
        Comment comment = new Comment();
        comment.setId(genUniqueKey());
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setTarget(target);
        comment.setCreateTime(date);
        mongoTemplate.insert(comment);
        return true;
    }

    /**
     * 新增一条评论
     * @param userId 评论人唯一标识
     * @param target 评论对象唯一标识
     * @param score 评分
     * @param content 评论内容
     * @return
     */
    public Boolean add(String userId,String target,String score,String content){
        Date date = new Date();
        Comment comment = new Comment();
        comment.setId(genUniqueKey());
        comment.setUserId(userId);
        comment.setScore(score);
        comment.setContent(content);
        comment.setTarget(target);
        comment.setCreateTime(date);
        mongoTemplate.insert(comment);
        return true;
    }

    /**
     * 新增评论
     * @param userId 评论人唯一标识
     * @param target 评论对象唯一标识
     * @param content 评论内容
     * @param img 评论图片
     * @return
     */
    public Boolean add(String userId,String target,String content,String[] img){
        Date date = new Date();
        Comment comment = new Comment();
        comment.setId(genUniqueKey());
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setTarget(target);
        comment.setImg(img);
        comment.setCreateTime(date);
        mongoTemplate.insert(comment);
        return true;
    }

    /**
     * 查找评论
     * @param id
     * @return
     */
    public Comment findById(String id){
        return mongoTemplate.findById(id,Comment.class);
    }

    /**
     * 根据主题查找评论
     * @param target
     * @return
     */
    public List<Comment> findByTarget(String target){
        Query query = new Query(Criteria.where("target").is(target));
        return mongoTemplate.find(query,Comment.class);
    }

    /**
     * 回复评论
     * @param id
     * @param replyUserId
     * @param content
     * @return
     */
    public Boolean reply(String id,String replyUserId ,String content){
        Date date = new Date();
        //构造评论
        Reply reply = new Reply();
        reply.setId(genUniqueKey());
        reply.setUserId(replyUserId);
        reply.setContent(content);
        reply.setCreateTime(date);
        //查找出评论
        Comment comment = mongoTemplate.findById(id,Comment.class);
        if(comment.getReplies()==null){
            List<Reply> replyList = new ArrayList<>();
            replyList.add(reply);
            comment.setReplies(replyList);
        }else{
            comment.getReplies().add(reply);
        }
        mongoTemplate.save(comment);
        return true;
    }

    /**
     * 点赞，支持
     * @param id
     * @param praiseUserId
     * @return
     */
    public Boolean praise(String id,String praiseUserId){
        Comment comment = mongoTemplate.findById(id,Comment.class);
        if(comment==null){
            return false;
        }
        List<Praise> commentPraises = comment.getPraises();
        if(commentPraises == null){
            commentPraises = new ArrayList<>();
        }
        //判断该用户是否已经点赞
        if(isPraise(commentPraises,praiseUserId)){
            return false;
        }
        Praise praise = new Praise();
        praise.setId(genUniqueKey());
        praise.setUserId(praiseUserId);
        praise.setIsPraise(true);
        praise.setCreateTime(new Date());
        commentPraises.add(praise);
        comment.setPraises(commentPraises);
        mongoTemplate.save(comment);
        return true;
    }

    /**
     * 查找用户是否已经点赞
     * @param id
     * @param praiseUserId
     * @return
     */
    public Boolean isPraise(String id,String praiseUserId){
        Comment comment = mongoTemplate.findById(id,Comment.class);
        if(comment==null){
            return false;
        }
        List<Praise> commentPraises = comment.getPraises();
        //判断该用户是否已经点赞
        return isPraise(commentPraises,praiseUserId);
    }

    /**
     * 判断是否已经点赞
     * @param commentPraises
     * @param praiseUserId
     * @return
     */
    private Boolean isPraise(List<Praise> commentPraises,String praiseUserId){
        if(commentPraises==null){
            return false;
        }
        //判断该用户是否已经点赞
        for(Praise praise: commentPraises){
            if(praise.getUserId().equals(praiseUserId)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取点赞数
     * @param id
     * @return
     */
    public Integer getPraiseCount(String id){
        Comment comment = mongoTemplate.findById(id,Comment.class);
        if(comment==null||comment.getPraises()==null){
            return null;
        }
        return comment.getPraises().size();
    }

    /**
     * 获取点赞列表
     * @param id
     * @return
     */
    public List<Praise> getPraise(String id){
        Comment comment = mongoTemplate.findById(id,Comment.class);
        if(comment==null||comment.getPraises()==null){
            return null;
        }
        return comment.getPraises();
    }

    /**
     * 生成唯一的主键
     * 格式: 时间+随机数
     * @return
     */
    private  synchronized String genUniqueKey() {
        Random random = new Random();
        Integer number = random.nextInt(900000) + 100000;
        return System.currentTimeMillis() + String.valueOf(number);
    }
}
